package com.service.schedule.ent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.DateUtils;
import com.common.StringUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.common.model.Global;
import com.dao.supply.CorpDao;
import com.dao.supply.InfoMapper;
import com.dao.supply.SupplyBankDao;
import com.dao.sys.SysUserMapper;
import com.enums.WorkFlowIdModel;
import com.model.supply.CorpModel;
import com.model.supply.SupplyBankModel;
import com.model.supply.SupplyInfoModel;
import com.model.supply.SupplyInfoVoModel;
import com.service.external.oa.GetWFStatusService;
import com.service.external.sap.EntService;

@Service("SupplyTaskService")
public class SupplyTaskService {
	static final Logger logger = LoggerFactory.getLogger(SupplyTaskService.class);
	@Autowired
	EntService entService;
	@Autowired
	InfoMapper infoMapper;
	@Autowired
	GetWFStatusService getWFStatusService;
	@Autowired
	GetWFStatusService wfStatusService;
	@Autowired
	SupplyBankDao bankDao;
	@Autowired
	CorpDao corpDao;
	@Autowired
	SysUserMapper sysUserMapper;

	protected void callOa() {

		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("oaStatus", Global.YES);
		queryMap.put("oaTime", DateUtils.addDays(DateUtils.getCurrentDate(), -60));
		List<SupplyInfoModel> list = infoMapper.selectList(queryMap);

		for (SupplyInfoModel infoModel : list) {
			SupplyInfoVoModel infoVoModel = new SupplyInfoVoModel();
			BeanUtils.copyProperties(infoModel, infoVoModel);

			if (StringUtils.isNotNull(infoModel.getOaFlowId() + "")) {
				infoModel.setUpdateId("1");
				infoModel.setUpdateName("admin");
				infoModel.setUpdateTime(DateUtils.getCurrentTime());
				// oa未审批完成
				if (getWFStatusService.getWFStatus(infoModel.getOaFlowId() + "", infoModel.getInfGroupCode())
						.getCode() != HttpStatus.SUCCESS_CODE) {
					// oa退回
					if (getWFStatusService.isBack(infoModel.getInfGroupCode(), WorkFlowIdModel.supplyWxId,
							infoModel.getOaFlowId() + "").getCode() == HttpStatus.SUCCESS_CODE) {
						infoModel.setInfApprovalStatus(Global.NO + "");
						infoModel.setStatus(Global.NO + "");
						infoModel.setOaStatus(Global.NO + "");
						infoMapper.update(infoModel);
					}
					// 已办流程没有
					if (getWFStatusService
							.getHendledWorkflowRequestList(infoModel.getOaFlowId() + "", infoModel.getInfGroupCode())
							.getCode() != HttpStatus.SUCCESS_CODE) {
						// 待办流程没有
						if (getWFStatusService
								.getToDoWorkflowRequestList(infoModel.getOaFlowId() + "", infoModel.getInfGroupCode())
								.getCode() != HttpStatus.SUCCESS_CODE) {
							infoModel.setInfApprovalStatus(Global.NO + "");
							infoModel.setStatus(Global.NO + "");
							infoModel.setOaStatus(Global.NO + "");
							infoMapper.update(infoModel);
						}
					}
					continue;
				} else {
					List<SupplyBankModel> bankModels = bankDao.selectByInfId(infoModel.getInfId());
					infoVoModel.setBankModels(bankModels);
					List<CorpModel> corpModels = corpDao.selectByInfId(infoModel.getInfId());
					infoVoModel.setCorpModels(corpModels);
					// 推送sap
					HttpResult sapHttpResult = entService.addEntToSap(infoVoModel);
					if (sapHttpResult.getCode() == HttpStatus.SUCCESS_CODE) {
						infoModel.setInfCode(sapHttpResult.getData().toString());
						infoModel.setOaStatus(Global.NUMERICAL_TWO + "");
						infoMapper.update(infoModel);
					}
				}
			}

		}

	}
}
